// 获取路由参数
export const getRouteParams = (props = {}) => {
  const { match = {} } = props;
  const { params = {} } = match;
  return params;
};

// 获取查询参数
export const getSearchParams = (search = '') => {
  const searchParams = {};

  if (search) {
    const params = search.slice(search.indexOf('?') + 1).split('&');
    params.forEach(item => {
      const param = item.split('=');
      searchParams[param[0]] = param[1];
    });
  }

  return searchParams;
};

// IScroll点击参数判断
export const getIScrollClick = () => {
  if (/iPhone|iPad|iPod|Macintosh/i.test(navigator.userAgent)) return false;
  if (/Chrome/i.test(navigator.userAgent)) return /Android/i.test(navigator.userAgent);
  if (/Silk/i.test(navigator.userAgent)) return false;
  if (/Android/i.test(navigator.userAgent)) {
    const s = navigator.userAgent.substr(navigator.userAgent.indexOf('Android') + 8, 3);
    return parseFloat(s[0] + s[3]) < 44 ? false : true;
  }
};

// 样式过滤器
export const cssFilter = (css = '') => {
  return css.replace(/(font-size:\s?\d+px)|(height:\s?\d+px)|(line-height:\s?\d+px)/g, () => '-cus-s: none');
};

// 打开链接
export const openLink = url => {
  if (window.em && window.em.checkJsApi('openLink')) {
    window.em.openLink({
      sysId: window.localStorage.emobile_ec_id,
      url: url,
      openType: 2
    });
  } else if (window.openLink && typeof window.openLink == 'function') {
    window.openLink(url);
  } else {
    window.open(url);
  }
};

// 修改标题
export const changeTitle = title => {
  document.title = title || '';
  if (window.em && window.em.checkJsApi('changeTitle')) {
    window.em.changeTitle({ title });
  }
};
